package com.example.universitylibraryback.controller;

import com.example.universitylibraryback.entity.BorrowRecord;
import com.example.universitylibraryback.service.BorrowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Tag(name = "借阅管理", description = "图书借阅、归还、续借等操作")
@RestController
@RequestMapping("/api/borrow")
public class BorrowController {
    
    @Autowired
    private BorrowService borrowService;
    
    @Operation(summary = "借阅图书", description = "读者借阅指定图书")
    @PostMapping
    public boolean borrowBook(@Parameter(description = "图书ID") @RequestParam("bookId") String bookId, 
                             @Parameter(description = "读者ID") @RequestParam("readerId") String readerId, 
                             @Parameter(description = "操作员角色") @RequestParam("operatorRole") String operatorRole) {
        return borrowService.borrowBook(bookId, readerId, operatorRole);
    }
    
    @Operation(summary = "归还图书", description = "读者归还借阅的图书")
    @PutMapping("/return")
    public boolean returnBook(@Parameter(description = "图书ID") @RequestParam("bookId") String bookId, 
                             @Parameter(description = "读者ID") @RequestParam("readerId") String readerId) {
        return borrowService.returnBook(bookId, readerId);
    }
    
    @Operation(summary = "续借图书", description = "读者续借已借阅的图书")
    @PutMapping("/renew/{recordId}")
    public boolean renewBook(@Parameter(description = "借阅记录ID") @PathVariable("recordId") String recordId) {
        return borrowService.renewBook(recordId);
    }
    
    @Operation(summary = "获取读者借阅记录", description = "查询指定读者的所有借阅记录")
    @GetMapping("/reader/{readerId}")
    public List<BorrowRecord> getBorrowRecordsByReaderId(@Parameter(description = "读者ID") @PathVariable("readerId") String readerId) {
        return borrowService.getBorrowRecordsByReaderId(readerId);
    }
    
    @Operation(summary = "获取逾期记录", description = "查询所有逾期未还的图书记录")
    @GetMapping("/overdue")
    public List<BorrowRecord> getOverdueRecords() {
        return borrowService.getOverdueRecords();
    }
    
    @Operation(summary = "获取借阅记录详情", description = "根据ID查询借阅记录详情")
    @GetMapping("/{recordId}")
    public BorrowRecord getBorrowRecordById(@Parameter(description = "借阅记录ID") @PathVariable("recordId") String recordId) {
        return borrowService.getBorrowRecordById(recordId);
    }
}